/*
* 特种设备详情
* */

import 'package:center_beijing/api/material_science_api.dart';
import 'package:center_beijing/api/task_api.dart';
import 'package:center_beijing/common/logutil.dart';
import 'package:center_beijing/common/notification.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/config/global_config.dart';
import 'package:center_beijing/widgets/custom_loading_widget.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

class SpecialEquipment extends StatefulWidget {
  @override
  _SpecialEquipmentState createState() => _SpecialEquipmentState();
}

class _SpecialEquipmentState extends State<SpecialEquipment>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  CustomLoading loading = CustomLoading.withMessage('提交中...');
  var businessId = Get.arguments['businessId'];
  var alertId = Get.arguments['alertId'];
  var taskId = Get.arguments['taskId'];
  var compDetail = Map();
  @override
  void initState() {
    _controller = AnimationController(vsync: this);
    super.initState();
    this.getAllMaterialRequirement();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  /*
  * 获取特种设备详情
  * */
  getAllMaterialRequirement() async {
    var pass = {'id': businessId};
    var result = await getEquipmentCheckAlert(pass);
    if (result != null) {
      this.setState(() {
        compDetail = result;
      });
    }
    LogUtil.d(result);
  }



/*
  * 确认
  * */
  submit() async {
    loading.show(delay: 30);
    var pass = {'alertId': businessId,'taskId':taskId};
    var result = await saveAlertApprove(pass);
    loading.dismiss();
    if (result != null&& result['errorMessage'] == null) {
      Fluttertoast.showToast(msg: '确认成功');
      bus.emit('refreshHome');
      Get.back();

    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("设备详情"),
      ),
      body: Container(
        color: Styles.MainBackgroundColor,
        padding:
        EdgeInsets.fromLTRB(0, 0, 0, MediaQuery.of(context).padding.bottom+70),
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.fromLTRB(Styles.spacing4,
                  Styles.spacing4, Styles.spacing4, Styles.spacing4),
              color: Styles.GranColor,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                      flex: 1,
                      child: Text(
                        '设备类型',
                        style: TextStyle(
                            color: Styles.DetailColor,
                            fontSize: Styles.NormalFont),
                      )),
                  Expanded(
                    flex: 2,
                    child: Text(
                      (compDetail['typeName'] != null)
                          ? compDetail['typeName']
                          : '--',
                      style: TextStyle(
                          color: Styles.DetailColor,
                          fontSize: Styles.NormalFont),
                    ),
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(Styles.spacing4,
                  Styles.spacing4, Styles.spacing4, Styles.spacing4),
              color: Styles.White70,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                      flex: 1,
                      child: Text(
                        '设备名称',
                        style: TextStyle(
                            color: Styles.DetailColor,
                            fontSize: Styles.NormalFont),
                      )),
                  Expanded(
                    flex: 2,
                    child: Text(
                      (compDetail['equipmentName'] != null)
                          ? compDetail['equipmentName']
                          : '--',
                      style: TextStyle(
                          color: Styles.DetailColor,
                          fontSize: Styles.NormalFont),
                    ),
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(Styles.spacing4,
                  Styles.spacing4, Styles.spacing4, Styles.spacing4),
              color: Styles.GranColor,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                      flex: 1,
                      child: Text(
                        '设备编号',
                        style: TextStyle(
                            color: Styles.DetailColor,
                            fontSize: Styles.NormalFont),
                      )),
                  Expanded(
                    flex: 2,
                    child: Text(
                      "${compDetail['machineEquipmentInfoEx']!=null?compDetail['machineEquipmentInfoEx']['code']:'--'}",
                      style: TextStyle(
                          color: Styles.DetailColor,
                          fontSize: Styles.NormalFont),
                    ),
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(Styles.spacing4,
                  Styles.spacing4, Styles.spacing4, Styles.spacing4),
              color: Styles.White70,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                      flex: 1,
                      child: Text(
                        '生产厂商',
                        style: TextStyle(
                            color: Styles.DetailColor,
                            fontSize: Styles.NormalFont),
                      )),
                  Expanded(
                    flex: 2,
                    child: Text(
                      "${compDetail['machineEquipmentInfoEx']!=null?compDetail['machineEquipmentInfoEx']['producer']:'--'}",
                      style: TextStyle(
                          color: Styles.DetailColor,
                          fontSize: Styles.NormalFont),
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: Container(
          padding: EdgeInsets.fromLTRB(
              Styles.spacing8, Styles.spacing8, Styles.spacing8, 10),
          child: Container(
            decoration: new BoxDecoration(
              //背景
              color: Styles.BlueColor,
              //设置四周圆角 角度
              borderRadius: BorderRadius.all(Radius.circular(12.0)),
            ),
            child: FlatButton(
              disabledColor: Styles.SelectColor,
              onPressed: () {
                FocusScope.of(context).requestFocus(new FocusNode());
                showDialog(
                    barrierDismissible: false,
                    context: this.context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        title: Text('注意'),
                        content: Text('确认吗？'),
                        actions: <Widget>[
                          new Container(
                            child: FlatButton(
                              child: new Text(
                                "取消",
                                style: TextStyle(color: Colors.black),
                              ),
                              onPressed: () {
                                Navigator.pop(context);
                              },
                            ),
                          ),
                          new Container(
                            child: FlatButton(
                              child: new Text(
                                "确定",
                                style: TextStyle(color: Colors.red),
                              ),
                              onPressed: () {
                                Navigator.pop(context);
this.submit();
                              },
                            ),
                          ),
                        ],
                      );
                    });

              },
              child: Container(
                height: Styles.MainButtonHeight,
                alignment: Alignment.center,
                child: Text(
                  '提醒确认',
                  style: TextStyle(
                      fontSize: Styles.MainButtonTextFont, color: Colors.white),
                ),
              ),
            ),
          ),
        )
    );
  }
}
